package com.gklyj.controller.administer;

import com.alibaba.fastjson.JSONObject;
import com.gklyj.pojo.User;
import com.gklyj.pojo.Zhanghao;
import com.gklyj.pojo.user_zhanghao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@Transactional
public class Zp_userController {

    @Autowired
    private com.gklyj.service.zhanghaoService zhanghaoService;

    @Autowired
    private com.gklyj.service.userService userService;

    @Autowired
    private com.gklyj.service.user_zhanghaoService user_zhanghaoService;


    //用于管理员后台跳转招聘单位信息管理页面
    @GetMapping("/tozp_user")
    public ModelAndView toZp_User(){
        ModelAndView modelAndView = new ModelAndView("administer/zp-user");
        return modelAndView;
    }

    //用于招聘单位信息管理页面，查找所有招聘单位信息并返回给  招聘单位信息管理页面
    @GetMapping("/findList_Zp")
    @ResponseBody
    public List<User> findList_User(){
        List<User> userList = userService.findUserByRole("recruiter");
        return userList;
    }

    /*
       根据前端传入数据进行搜索
        */
    @PostMapping("/zp_search")
    @ResponseBody
    public List<User> searchQz_user(@RequestBody User user){
        System.out.println(user);
        List<User> ZpuserList = userService.searchZpUser(user);
        System.out.println(ZpuserList);
        return ZpuserList;
    }

    /*
跳转编辑页面
 */
    @GetMapping("/tozpEdit")
    public String qzEditPage(@RequestParam("id") String id, Model model){
        System.out.println("========================================================="+id);
        User user = userService.findUserById(Integer.valueOf(id));
        //ModelAndView modelAndView = new ModelAndView("administer/table/edit");
        model.addAttribute("user1",user);
        //modelAndView.addObject("obj",user);
        return "administer/table/edit2";
    }


    /*
    跳转添加信息页面
     */
    @GetMapping("/tozpAdd")
    public String toQzAddPage(){
        return "administer/table/add2";
    }

    /*
    添加招聘单位用户信息
     */
    @PostMapping("/addZpUser")
    @ResponseBody
    public Boolean addQz(@RequestBody String data){
        //将数据转化为json格式
        JSONObject json = JSONObject.parseObject(data);
        User user = new User();
        Zhanghao zhanghao = new Zhanghao();
        user_zhanghao userZhanghao = new user_zhanghao();
        //user信息
        user.setName((String) json.get("name"));
        user.setAge(json.getInteger("age"));
        user.setSex((String) json.get("sex"));
        user.setPhone_number((String) json.get("phone_number"));
        user.setDepartment((String) json.get("department"));
        boolean r = userService.save(user);

        //zhanghao信息
        zhanghao.setUsername((String) json.get("username"));
        zhanghao.setPassword((String) json.get("password"));
        zhanghao.setRole("recruiter");
        boolean r1 = zhanghaoService.save(zhanghao);

        int u_id = userService.findU_id((String) json.get("name"),(String) json.get("phone_number"));
        int z_id = zhanghaoService.findZ_id((String) json.get("username"),(String) json.get("password"));
        userZhanghao.setU_id(u_id);
        userZhanghao.setZ_id(z_id);
        boolean r2 = user_zhanghaoService.save(userZhanghao);
        if (r && r1 && r2){
            return true;
        }else {
            return false;
        }

    }
}
